import {useRouterHistory, browserHistory, hashHistory} from 'react-router';

import { createHashHistory } from 'history';

export const getBasename = (history, dirname) => {

    var file = document
        .getElementsByTagName('link')[0]
        .href
        .substr(
            location
            .origin
            .length
        );
    var pathname =  location.pathname;

    var fileDirs = file.split('/');
    var pathDirs = pathname.split('/');

    var len  = Math.min(fileDirs.length, pathDirs.length);

    var result = [];

    for(var i = 0; i < len; i ++) {

        if(fileDirs[i] !== pathDirs[i]) break;
        result.push(fileDirs[i]);
    }

    var basename = result.join('/');

    if(!basename || basename === '') {
        basename = '/'
    } else {
        basename = basename + '/';
    }

    return basename;
};

// history
export const appbrowserHistory = useRouterHistory(() => browserHistory)({basename: getBasename()});

//export const appHashHistory = useRouterHistory(() => hashHistory)({queryKey: false});



export const appHashHistory = useRouterHistory(() => createHashHistory)({queryKey: false});
